/**
 * @author Ahammer
 * @date   2009 07
 * @update 
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package opgui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JSpinner;
import javax.swing.JToolBar;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.InternationalFormatter;
import main.IqmTools;
import main.Tank;

public class IqmFracPyramidGUI extends IqmOperatorJFrame implements IqmOperatorGUIBuilder,
															   ActionListener, ChangeListener {

	private  ParameterBlockJAI     pbJAI       = null;  //  @jve:decl-index=0:
	//public  ImgProcFuncJFrame    opGUIFrame  = null;
	//private  IqmFracPyramidGUI   opGUIFrame  = null;  //actionPerformed events werden in dieser Klasse abgehandelt
 
	private int numPyrImgs = 0;
 	 
	private JPanel 				  jPanelNumPyrImgs    = null;	
	private JLabel				  jLabelNumPyrImgs	  = null;
	private JSpinner			  jSpinnerNumPyrImgs  = null;
	
	private JRadioButtonMenuItem  buttNN         = null;	//nearest neighbor   filtreredsubsample
	private JRadioButtonMenuItem  buttBL         = null;	//bilinear
	private JRadioButtonMenuItem  buttBC         = null;	//bicubic
	private JRadioButtonMenuItem  buttBC2        = null;	//bicubic2
	private JRadioButtonMenuItem  buttAV 	     = null;	//average subsampleaverage
	private JRadioButtonMenuItem  buttIntPHidden = null;
	private JToolBar              jToolBarIntP   = null;
	private ButtonGroup           buttGroupIntP  = null;
	
	private JPanel 		jPanelRegression	= null;	
	private JLabel		jLabelRegression  	= null;;
	private JPanel 		jPanelRegStart   	= null;	
	private JLabel		jLabelRegStart	 	= null;
	private JSpinner	jSpinnerRegStart 	= null;	
	private JPanel 		jPanelRegEnd    	= null;	
	private JLabel		jLabelRegEnd		= null;
	private JSpinner	jSpinnerRegEnd  	= null;
	
	private  JCheckBox	jCheckBoxShowPlot			= null;
	private  JCheckBox	jCheckBoxDeleteExistingPlot = null;

	
	/**
	 * This method sets the current parameter block to the argument
	 * @param ParameterBlockJAI pb
	 *
	 */
	@Override
	public void setParameterBlock(ParameterBlockJAI arg){
		pbJAI = arg;
		IqmTools.setCurrParameterBlockJAI(pbJAI);	
	}
	/**
	 * This method sets the current parameter block
	 * The individual values of the GUI  current ParameterBlock
	 *
	 */
	@Override
	public void setParameterBlock(){
		
		pbJAI.setParameter("NumPyrImgs",  ((Number)jSpinnerNumPyrImgs.getValue()).intValue());
		
		if (buttNN.isSelected())  pbJAI.setParameter("Interpolation", 0);
		if (buttBL.isSelected())  pbJAI.setParameter("Interpolation", 1);
		if (buttBC.isSelected())  pbJAI.setParameter("Interpolation", 2);
		if (buttBC2.isSelected()) pbJAI.setParameter("Interpolation", 3);
		if (buttAV.isSelected())  pbJAI.setParameter("Interpolation", 4);

		pbJAI.setParameter("RegStart", ((Number)jSpinnerRegStart.getValue()).intValue());
		pbJAI.setParameter("RegEnd",   ((Number)jSpinnerRegEnd.getValue()).intValue());

		if (jCheckBoxShowPlot.isSelected())  			pbJAI.setParameter("ShowPlot", 1);
		if (!jCheckBoxShowPlot.isSelected()) 			pbJAI.setParameter("ShowPlot", 0);
		if (jCheckBoxDeleteExistingPlot.isSelected()) 	pbJAI.setParameter("DeleteExistingPlot", 1);
		if (!jCheckBoxDeleteExistingPlot.isSelected())	pbJAI.setParameter("DeleteExistingPlot", 0);

		IqmTools.setCurrParameterBlockJAI(pbJAI);	
	}
	
	/**
	 * This method sets the current parameter values
	 *
	 */
	@Override
	public void setParameterValues(){
		int idx = IqmTools.getCurrManagerImgIndex();
	    PlanarImage pi = Tank.getCurrentTankImageAt(idx); 
	       if (pi == null){
	    	   System.out.println("IqmFracPyramidGUI: Current image not defined");
	    	   return;
	       }
//	    int pixelSize = pi.getColorModel().getPixelSize();
//      int numBands  = pi.getNumBands(); 
//      String type = IqmTools.getCurrImgTyp(pixelSize, numBands);
//      String type = IqmTools.getCurrImgTyp(pi);

		buttIntPHidden.setSelected(true);
		
		
	    jSpinnerNumPyrImgs.removeChangeListener(this);	
	    jSpinnerNumPyrImgs.setValue(pbJAI.getIntParameter("NumPyrImgs"));
	    jSpinnerNumPyrImgs.addChangeListener(this);
	    	    

		if (pbJAI.getIntParameter("Interpolation") == 0)  buttNN.setSelected(true);
		if (pbJAI.getIntParameter("Interpolation") == 1)  buttBL.setSelected(true);
		if (pbJAI.getIntParameter("Interpolation") == 2)  buttBC.setSelected(true);
		if (pbJAI.getIntParameter("Interpolation") == 3)  buttBC2.setSelected(true);
		if (pbJAI.getIntParameter("Interpolation") == 4)  buttAV.setSelected(true);
		
	    jSpinnerRegStart.removeChangeListener(this);	
	    jSpinnerRegStart.setValue(pbJAI.getIntParameter("RegStart"));
	    jSpinnerRegStart.addChangeListener(this);
	    
	    jSpinnerRegEnd.removeChangeListener(this);	
	    jSpinnerRegEnd.setValue(pbJAI.getIntParameter("RegEnd"));
	    jSpinnerRegEnd.addChangeListener(this);
	    
	    if (pbJAI.getIntParameter("ShowPlot") == 0)   jCheckBoxShowPlot.setSelected(false);
	    if (pbJAI.getIntParameter("ShowPlot") == 1)   jCheckBoxShowPlot.setSelected(true);
	    if (pbJAI.getIntParameter("DeleteExistingPlot") == 0)   jCheckBoxDeleteExistingPlot.setSelected(false);
	    if (pbJAI.getIntParameter("DeleteExistingPlot") == 1)   jCheckBoxDeleteExistingPlot.setSelected(true);

	}
	/**
	 * constructor
	 */
	public IqmFracPyramidGUI() {

	}
	/**
	 * This method creates and shows the GUI
	 * 
	 */
	@Override
	public void createIqmOperatorGUI() {
		//opGUIFrame = new ImgProcFuncJFrame();  //actionPerforemd events werden in dieser Klasse abgehandelt
		//opGUIFrame = new IqmFracPyramidGUI();      //actionPerforemd events werden in dieser Klasse abgehandelt
		//this.getJContentPane();
		this.initialize(); //Aufruf von ImgProcFuncJFrame
		this.setTitle("FracPyramid"); 
	
		PlanarImage pi = Tank.getCurrentTankImageAt(IqmTools.getCurrManagerImgIndex());
		int width  = pi.getWidth();
		int height = pi.getHeight();
		numPyrImgs = this.getMaxPyramidNumber(width,  height);
		
		//Dimension dim = this.getSize();
		//this.setSize(dim.width, dim.height+150);
		this.getJContentPane().add(getJPanelNumPyrImgs(), getGridBagConstraintsNumPyrImgs());
		this.getJContentPane().add(getJToolBarIntP(),     getGridBagConstraintsIntP());
		this.getJContentPane().add(getJPanelRegression(), getGridBagConstraintsRegression());
		this.getJContentPane().add(getJCheckBoxShowPlot(),				getGridBagConstraintsShowPlot());
		this.getJContentPane().add(getJCheckBoxDeleteExistingPlot(),    getGridBagConstraintsDeleteExistingPlot());	

		//this.repaint();
		if (pbJAI == null) pbJAI = new ParameterBlockJAI("IqmFracPyramid");  //nur wenn das erste mal aufgerufen
		//pbJAI = new ParameterBlockJAI("IqmFracPyramid"); //immer der default Block
		IqmTools.setCurrParameterBlockJAI(pbJAI);
		
		setParameterValues();
		//Set image dependent initial values;
		
		//System.out.println("IqmFracPyramidGUI: numberMax: " + numberMax);
		SpinnerModel sModel = new SpinnerNumberModel(numPyrImgs, 2, numPyrImgs, 1); //init, min, max, step
		jSpinnerNumPyrImgs.removeChangeListener(this);
		jSpinnerNumPyrImgs.setModel(sModel);
	    jSpinnerNumPyrImgs.setValue(numPyrImgs);
	    jSpinnerRegEnd.removeChangeListener(this);	
	    jSpinnerRegEnd.setValue(numPyrImgs);
	    jSpinnerRegEnd.addChangeListener(this);
		JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerNumPyrImgs.getEditor()  ;
		JFormattedTextField ftf = defEditor.getTextField() ;
		ftf.setEditable(false);
		InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
		DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
		decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;	
	    jSpinnerNumPyrImgs.addChangeListener(this);
	
	    setParameterBlock();
		this.pack();
	}

	private int getMaxPyramidNumber(int width, int height) { //inclusive original image
		float oldWidth = (float)width;
		float oldHeight = (float)height;
		int number = 1; //inclusive original image
		boolean abbruch = false;
		while (!abbruch){
			float  newWidth = oldWidth/2;
			float newHeight = oldHeight/2;
			if ((newWidth < 1.0) || (newHeight < 1.0)){
				abbruch = true;
			} else{
				oldWidth  = newWidth;
				oldHeight = newHeight;
//				System.out.println("IqmFracPyramidGUI: newWidth: " +  newWidth);
//				System.out.println("IqmFracPyramidGUI: newHeight: " +  newHeight);
				number = number + 1;
			}		
		}
		return number;
	}
	private  GridBagConstraints getGridBagConstraintsNumPyrImgs(){
		GridBagConstraints gridBagConstraintsNumPyrImgs = new GridBagConstraints();
		gridBagConstraintsNumPyrImgs.gridx = 1;
		gridBagConstraintsNumPyrImgs.gridy = 5;
		gridBagConstraintsNumPyrImgs.gridwidth = 1;//?
		gridBagConstraintsNumPyrImgs.insets = new Insets(10,0,20,0);  //top left bottom right
		//gridBagConstraintsNumPyrImgs.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsNumPyrImgs;
	}
	private  GridBagConstraints getGridBagConstraintsIntP(){
		GridBagConstraints gridBagConstraintsIntP = new GridBagConstraints();
		gridBagConstraintsIntP.gridx = 2;
		gridBagConstraintsIntP.gridy = 5;
		gridBagConstraintsIntP.gridwidth = 2;//?
		gridBagConstraintsIntP.insets = new Insets(10,0,20,0);  //top left bottom right
		//gridBagConstraintsIntP.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsIntP;
	}
	private  GridBagConstraints getGridBagConstraintsRegression(){
		GridBagConstraints gridBagConstraintsRegression = new GridBagConstraints();
		gridBagConstraintsRegression.gridx = 1;
		gridBagConstraintsRegression.gridy = 6;
		gridBagConstraintsRegression.gridwidth = 3;//?
		gridBagConstraintsRegression.insets = new Insets(10,0,0,0);  //top left bottom right
		//gridBagConstraintsRegression.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsRegression;
	}
	private  GridBagConstraints getGridBagConstraintsShowPlot(){
		GridBagConstraints gridBagConstraintsShowPlot = new GridBagConstraints();
		gridBagConstraintsShowPlot.gridx = 1;
		gridBagConstraintsShowPlot.gridy = 7;
		gridBagConstraintsShowPlot.anchor = GridBagConstraints.LINE_START;
		gridBagConstraintsShowPlot.insets = new Insets(10,20,10,0); //top left bottom right
		//gridBagConstraintsShowPlot.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsShowPlot;
	}
	private  GridBagConstraints getGridBagConstraintsDeleteExistingPlot(){
		GridBagConstraints gridBagConstraintsDeleteExistingPlot = new GridBagConstraints();
		gridBagConstraintsDeleteExistingPlot.gridx = 2;
		gridBagConstraintsDeleteExistingPlot.gridy = 7;
		gridBagConstraintsDeleteExistingPlot.anchor = GridBagConstraints.LINE_START;
		gridBagConstraintsDeleteExistingPlot.insets = new Insets(10,0,10,0); //top left bottom right
		//gridBagConstraintsDeleteExistingPlot.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsDeleteExistingPlot;
	}

	//------------------------------------------------------------------------------------------------------

	/**
	 * This method updates the GUI if needed
	 * This method overrides IqmOperatorJFrame
	 */
	public void updateIqmOperatorGUI(){
		
	}
	//----------------------------------------------------------------------------------------------------------
	/**
	 * This method initializes jJPanelNumPyrImgs	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelNumPyrImgs() {
		if (jPanelNumPyrImgs == null) {
			jPanelNumPyrImgs = new JPanel();
			jPanelNumPyrImgs.setLayout(new BorderLayout());		
			jLabelNumPyrImgs = new JLabel("Image number: ");
			//jLabelNumPyrImgs.setPreferredSize(new Dimension(70, 22));
			jLabelNumPyrImgs.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(2, 2, Integer.MAX_VALUE, 1); //init, min, max, step
			jSpinnerNumPyrImgs = new JSpinner(sModel);
			//jSpinnerNumPyrImgs = new JSpinner();
			jSpinnerNumPyrImgs.setPreferredSize(new Dimension(60, 22));
			jSpinnerNumPyrImgs.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerNumPyrImgs.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			ftf.setEditable(false);
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelNumPyrImgs.add(jLabelNumPyrImgs,   BorderLayout.NORTH);
			jPanelNumPyrImgs.add(jSpinnerNumPyrImgs, BorderLayout.CENTER);			
		}
		return jPanelNumPyrImgs;
	}
	//-------------------------------------------------------------------------------------------
	/**
	 * This method initializes the Option: Nearest Neighbor	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtNN() {
		//if (buttNN == null) {
			buttNN  = new JRadioButtonMenuItem();
			buttNN .setText("Nearest Neighbor");
			buttNN .setToolTipText("uses FilteredSubSample and Nearest Neighbor resampling");
			buttNN .addActionListener(this);
			buttNN.setActionCommand("parameter");
		//}
		return buttNN;
	}
	/**
	 * This method initializes the Option: Bilinear interpolation
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtBL() {
		//if (buttBL == null) {
			buttBL  = new JRadioButtonMenuItem();
			buttBL .setText("Bilinear");
			buttBL .setToolTipText("uses FilteredSubSample and Bilinear interpolation");
			buttBL .addActionListener(this);
			buttBL.setActionCommand("parameter");
		//}
		return buttBL;
	}
	/**
	 * This method initializes the Option: Bicubic interpolation	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtBC() {
		//if (buttBC == null) {
			buttBC  = new JRadioButtonMenuItem();
			buttBC .setText("Bicubic");
			buttBC .setToolTipText("uses FilteredSubSample and Bicubic interpolation");
			buttBC .addActionListener(this);
			buttBC.setActionCommand("parameter");
		//}
		return buttBC;
	}
	/**
	 * This method initializes the Option: Bicubic2 interpolation	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtBC2() {
		//if (buttBC2 == null) {
			buttBC2  = new JRadioButtonMenuItem();
			buttBC2 .setText("Bicubic2");
			buttBC2 .setToolTipText("uses FilteredSubSample and Bicubic2 interpolation");
			buttBC2 .addActionListener(this);
			buttBC2.setActionCommand("parameter");
		//}
		return buttBC2;
	}
	/**
	 * This method initializes the Option: subsampleaverage	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtAV() {
		//if (buttAV == null) {
			buttAV  = new JRadioButtonMenuItem();
			buttAV .setText("Average");
			buttAV .setToolTipText("uses SubsampleAverage");
			buttAV .addActionListener(this);
			buttAV.setActionCommand("parameter");
		//}
		return buttAV;
	}
	
	/**
	 * This method initializes the hidden option	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMenuButtIntPHidden() {
		//if (buttIntPHidden == null) {
			buttIntPHidden  = new JRadioButtonMenuItem();
			buttIntPHidden .setText("Hidden");
			buttIntPHidden.setVisible(false);
		//}
		return buttIntPHidden;
	}


	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private   JToolBar getJToolBarIntP() {
//		if (jToolBarIntP== null) {
			jToolBarIntP = new JToolBar("Options");
			jToolBarIntP.setOrientation(1); // 0 Horizontal 1 Vertical orientation
			jToolBarIntP.add(getJRadioButtonMenuButtNN());
			jToolBarIntP.add(getJRadioButtonMenuButtBL());
			jToolBarIntP.add(getJRadioButtonMenuButtBC());
			jToolBarIntP.add(getJRadioButtonMenuButtBC2());
			jToolBarIntP.add(getJRadioButtonMenuButtAV());
			jToolBarIntP.add(getJRadioButtonMenuButtIntPHidden());
			//jToolBarIntP.addSeparator();
			this.setButtonGroupIntP();	//Gruppieren der JRadioButtons
//		}
		return jToolBarIntP;
	}

	private  void setButtonGroupIntP() {
		//if (ButtonGroup buttGroup == null) {
			buttGroupIntP = new ButtonGroup();
			buttGroupIntP.add(buttNN);
			buttGroupIntP.add(buttBL);
			buttGroupIntP.add(buttBC);
			buttGroupIntP.add(buttBC2);
			buttGroupIntP.add(buttAV);
			buttGroupIntP.add(buttIntPHidden);
	}
	   //-----------------------------------------------------------------------------------------
	/**
	 * This method initializes jJPanelRegression	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelRegression() {
		if (jPanelRegression== null) {
			jPanelRegression = new JPanel();
			jPanelRegression.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));				
			jPanelRegression.add(getJLabelRegression());
			jPanelRegression.add(getJPanelRegStart());
			jPanelRegression.add(getJPanelRegEnd());			
		}
		return jPanelRegression;
	}
	//------------------------------------------------------------------------------------------
	/**
	 * This method initializes jLabelRegression	
	 * 	
	 * @return javax.swing.JLabel
	 */
	private JLabel getJLabelRegression() {
		if (jLabelRegression == null){
			jLabelRegression = new JLabel("Regression:");
		}
		return jLabelRegression;	
	}
	//----------------------------------------------------------------------------------------------------------
	/**
	 * This method initializes jJPanelRegStart	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelRegStart() {
		if (jPanelRegStart == null) {
			jPanelRegStart = new JPanel();
			jPanelRegStart.setLayout(new FlowLayout());		
			jLabelRegStart = new JLabel("Start:");
			//jLabelRegStart.setPreferredSize(new Dimension(40, 22));
			jLabelRegStart.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(1, 1, numPyrImgs-1, 1); //init, min, max, step
			jSpinnerRegStart = new JSpinner(sModel);
			//jSpinnerRegStart = new JSpinner();
			jSpinnerRegStart.setPreferredSize(new Dimension(60, 22));
			jSpinnerRegStart.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerRegStart.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			ftf.setEditable(false);
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelRegStart.add(jLabelRegStart);
			jPanelRegStart.add(jSpinnerRegStart);			
		}
		return jPanelRegStart;
	}
	/**
	 * This method initializes jJPanelRegEnd	
	 * 	
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelRegEnd() {
		if (jPanelRegEnd == null) {
			jPanelRegEnd = new JPanel();
			jPanelRegEnd.setLayout(new FlowLayout());		
			jLabelRegEnd = new JLabel("End:");
			//jLabelRegEnd.setPreferredSize(new Dimension(40, 22));
			jLabelRegEnd.setHorizontalAlignment(JLabel.RIGHT);
			SpinnerModel sModel = new SpinnerNumberModel(numPyrImgs, 2, numPyrImgs, 1); //init, min, max, step
			jSpinnerRegEnd = new JSpinner(sModel);
			//jSpinnerRegEnd = new JSpinner();
			jSpinnerRegEnd.setPreferredSize(new Dimension(60, 22));
			jSpinnerRegEnd.addChangeListener(this);
			JSpinner.DefaultEditor defEditor = (JSpinner.DefaultEditor)jSpinnerRegEnd.getEditor()  ;
			JFormattedTextField ftf = defEditor.getTextField() ;
			ftf.setEditable(false);
			InternationalFormatter intFormatter = (InternationalFormatter)ftf.getFormatter() ;
			DecimalFormat decimalFormat = (DecimalFormat)intFormatter.getFormat() ;
			decimalFormat.applyPattern("#");  //decimalFormat.applyPattern("#,##0.0")  ;		
			jPanelRegEnd.add(jLabelRegEnd);
			jPanelRegEnd.add(jSpinnerRegEnd);			
		}
		return jPanelRegEnd;
	}
	//------------------------------------------------------------------------------------	
	/**
	 * This method initializes jCheckBoxShowPlot	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private  JCheckBox getJCheckBoxShowPlot() {
		if (jCheckBoxShowPlot == null) {
			jCheckBoxShowPlot = new JCheckBox();
			jCheckBoxShowPlot.setText("ShowPlot");
			jCheckBoxShowPlot.addActionListener(this);			
			jCheckBoxShowPlot.setActionCommand("parameter");			
		}
		return jCheckBoxShowPlot;
	}
	//------------------------------------------------------------------------------------
	/**
	 * This method initializes jCheckBoxDeleteExistingPlot	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private  JCheckBox getJCheckBoxDeleteExistingPlot() {
		if (jCheckBoxDeleteExistingPlot == null) {
			jCheckBoxDeleteExistingPlot = new JCheckBox();
			jCheckBoxDeleteExistingPlot.setText("DeleteExistingPlot");
			jCheckBoxDeleteExistingPlot.addActionListener(this);			
			jCheckBoxDeleteExistingPlot.setActionCommand("parameter");			
		}
		return jCheckBoxDeleteExistingPlot;
	}

    //----------------------------------------------------------------------------------------------------------
	public void actionPerformed(ActionEvent e) {
		//System.out.println("IqmFracPyramidGUI: event e: " +e.getActionCommand());
		if ("reset".equals(e.getActionCommand())) {
			ParameterBlockJAI pbJAI = new ParameterBlockJAI("IqmFracPyramid".toLowerCase());
			IqmTools.setCurrParameterBlockJAI(pbJAI);
			this.setParameterBlock(pbJAI);
			this.setParameterValues();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
			this.reset();
		}
		if ("parameter".equals(e.getActionCommand())) {		
			this.setParameterBlock();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		}

		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}

	}
	//-------------------------------------------------------------------------------------------------
	@Override
	public void stateChanged(ChangeEvent e) {
		
		numPyrImgs   = ((Number)jSpinnerNumPyrImgs.getValue()).intValue();
		int regStart = ((Number)jSpinnerRegStart.getValue()).intValue();
		int regEnd   = ((Number)jSpinnerRegEnd.getValue()).intValue();
		
		jSpinnerNumPyrImgs.removeChangeListener(this);
		jSpinnerRegStart.removeChangeListener(this);
		jSpinnerRegEnd.removeChangeListener(this);
		
		if (jSpinnerNumPyrImgs == e.getSource()) {
			//if(regEnd > numPyrImgs){
				jSpinnerRegEnd.setValue(numPyrImgs);
				regEnd = numPyrImgs; 
			//}
			if(regStart >= regEnd){
				jSpinnerRegStart.setValue(regEnd-1);
				regStart = regEnd-1;
			}
		}
		if (jSpinnerRegEnd == e.getSource()){
			if (regEnd > numPyrImgs){
				jSpinnerRegEnd.setValue(numPyrImgs);
				regEnd = numPyrImgs; 
			}
			if (regEnd <= regStart){
				jSpinnerRegEnd.setValue(regStart+1);
				regEnd = regStart+1; 
			}
		}
		if (jSpinnerRegStart == e.getSource()){
			if (regStart >= regEnd){
				jSpinnerRegStart.setValue(regEnd-1);
				regStart = regEnd-1;				
			}
		}
		
		jSpinnerNumPyrImgs.addChangeListener(this);
		jSpinnerRegStart.addChangeListener(this);
		jSpinnerRegEnd.addChangeListener(this);

		this.setParameterBlock();
		this.setParameterValues();
		//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}
		
	}
	//-------------------------------------------------------------------------------------------------
	public static void main() {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				IqmFracPyramidGUI gui = new IqmFracPyramidGUI();
			    gui.createIqmOperatorGUI();

			}
		});
	}
	
}//END
